package com.probox.common.entity.service.customer.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.*;
import java.util.stream.Collectors;

import com.probox.common.core.enums.DataStatus;
import com.probox.common.core.utils.SecurityUtils;
import com.probox.common.core.utils.StringUtils;
import com.probox.common.entity.domain.customer.CustomerHiddenLabel;
import com.probox.common.entity.mapper.customer.CustomerHiddenLabelMapper;
import com.probox.common.entity.service.customer.ICustomerHiddenLabelService;
import com.probox.common.entity.service.customer.ICustomerInfoService;
import com.probox.common.entity.service.product.IProductInfoService;
import com.probox.common.entity.service.product.IProductTagsService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 客户潜藏标签Service业务层处理
 *
 * @author ruoyi
 * @date 2023-02-04
 */
@Service
@Transactional
public class CustomerHiddenLabelServiceImpl extends ServiceImpl<CustomerHiddenLabelMapper, CustomerHiddenLabel> implements ICustomerHiddenLabelService {
    @Autowired
    private CustomerHiddenLabelMapper customerHiddenLabelMapper;

    @Autowired
    private IProductInfoService productInfoService;

    @Autowired
    private ICustomerInfoService customerInfoService;

    @Autowired
    private IProductTagsService productTagsService;


    /**
     * 统计这个客户的潜在标签
     *
     * @param customerId
     * @return
     */
    @Override
    public List<HashMap> selectHiddenTages (Long customerId){
        return customerHiddenLabelMapper.selectHiddenTages(customerId);
    }


    /**
     * 给用户带上潜在标签
     *
     * @param customerId
     * @param productId
     */
    @Override
    public void addOrUpdateLabel(Long customerId, String productType,Long productId,String tagsName) {
        if (customerId != null && StringUtils.isNotEmpty(tagsName)) {
            String[] tags = tagsName.split(",");
            List<CustomerHiddenLabel> list = new ArrayList<>();
            for(String tag:tags){
                CustomerHiddenLabel customerHiddenLabel = new CustomerHiddenLabel();
                customerHiddenLabel.setCreateName(SecurityUtils.getCustomerUserName());
                customerHiddenLabel.setCreateTime(DateUtil.date());
                customerHiddenLabel.setCreateUid(customerId);
                customerHiddenLabel.setDataStatus(DataStatus.valid.getKey());
                customerHiddenLabel.setLabelValue(tag);
                customerHiddenLabel.setProductId(productId);
                customerHiddenLabel.setProductType(productType);
                customerHiddenLabel.setCustomerId(customerId);
                customerHiddenLabel.setUpdateName(SecurityUtils.getCustomerUserName());
                customerHiddenLabel.setUpdateTime(DateUtil.date());
                customerHiddenLabel.setUpdateUid(customerId);
                list.add(customerHiddenLabel);
            }
            if (list.size()>0){
                saveBatch(list);
            }

        }
    }

    /**
     * 查询客户潜藏标签
     *
     * @param hiddenId 客户潜藏标签ID
     * @return 客户潜藏标签
     */
    @Override
    public CustomerHiddenLabel selectCustomerHiddenLabelById(Long hiddenId) {
        return this.getById(hiddenId);
    }

    /**
     * 根据客户id查询潜在标签
     * @param customerId
     * @return
     */
    @Override
    public List<CustomerHiddenLabel> selectLabelByCustomerId(Long customerId){
        QueryWrapper<CustomerHiddenLabel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("customer_id", customerId);
        queryWrapper.eq("data_status", DataStatus.valid.getKey());
        return  customerHiddenLabelMapper.selectList(queryWrapper);
    }

    /**
     * 查询客户潜藏标签列表
     *
     * @param customerHiddenLabel 客户潜藏标签
     * @return 客户潜藏标签
     */
    @Override
    public List<CustomerHiddenLabel> queryList(CustomerHiddenLabel customerHiddenLabel) {
        QueryWrapper<CustomerHiddenLabel> queryWrapper = Wrappers.query();
        if (StringUtils.isNotEmpty(String.valueOf(customerHiddenLabel.getHiddenId())) && customerHiddenLabel.getHiddenId() != null) {
            queryWrapper.eq("Hidden_id", customerHiddenLabel.getHiddenId());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerHiddenLabel.getCustomerId())) && customerHiddenLabel.getCustomerId() != null) {
            queryWrapper.eq("customer_id", customerHiddenLabel.getCustomerId());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerHiddenLabel.getLabelValue())) && customerHiddenLabel.getLabelValue() != null) {
            queryWrapper.eq("label_value", customerHiddenLabel.getLabelValue());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerHiddenLabel.getDataStatus())) && customerHiddenLabel.getDataStatus() != null) {
            queryWrapper.eq("DATA_STATUS", customerHiddenLabel.getDataStatus());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerHiddenLabel.getCreateUid())) && customerHiddenLabel.getCreateUid() != null) {
            queryWrapper.eq("CREATE_UID", customerHiddenLabel.getCreateUid());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerHiddenLabel.getUpdateUid())) && customerHiddenLabel.getUpdateUid() != null) {
            queryWrapper.eq("UPDATE_UID", customerHiddenLabel.getUpdateUid());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerHiddenLabel.getCreateName())) && customerHiddenLabel.getCreateName() != null) {
            queryWrapper.eq("CREATE_NAME", customerHiddenLabel.getCreateName());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerHiddenLabel.getUpdateName())) && customerHiddenLabel.getUpdateName() != null) {
            queryWrapper.eq("UPDATE_NAME", customerHiddenLabel.getUpdateName());
        }
        return customerHiddenLabelMapper.selectList(queryWrapper);
    }

    /**
     * 新增客户潜藏标签
     *
     * @param customerHiddenLabel 客户潜藏标签
     * @return 结果
     */
    @Override
    public boolean insertCustomerHiddenLabel(CustomerHiddenLabel customerHiddenLabel) {

        return this.save(customerHiddenLabel);

    }

    /**
     * 修改客户潜藏标签
     *
     * @param customerHiddenLabel 客户潜藏标签
     * @return 结果
     */
    @Override
    public boolean updateCustomerHiddenLabel(CustomerHiddenLabel customerHiddenLabel) {

        return this.updateById(customerHiddenLabel);
    }

    /**
     * 批量删除客户潜藏标签
     *
     * @param hiddenIds 需要删除的客户潜藏标签ID
     * @return 结果
     */
    @Override
    public boolean deleteCustomerHiddenLabelByIds(Long[] hiddenIds) {
        return this.removeByIds(Arrays.asList(hiddenIds));
    }


}
